import os
import time
import re

def mkSubFileByString(lines, srcName, insertStr):
    [des_filename, extname] = os.path.splitext(srcName)
    filename = des_filename + '_' + str(insertStr) + extname
    print('make file: %s' % filename)
    fout = open(filename, 'w', encoding='UTF-8')
    try:
        # fout.writelines([head])
        fout.writelines(lines)

    finally:
        fout.close()


def splitByLineString(filename, insertStr="AAA"):
    fin = open(filename, 'r', encoding='UTF-8')
    try:
        # head = fin.readline()
        buf = []
        for line in fin:
            if line == '\n':
                continue
            test1 = 'INSERT INTO "ZLHIS"'
            if not test1 in line:
                continue
            # if 'INSERT INTO "ZLHIS".'+insertStr+' VALUES' in 'INSERT INTO "ZLHIS"."AAA" VALUES':
            test2 = r'INSERT INTO "ZLHIS"."'+insertStr+r'" VALUES'
            if test2 in line:
                buf.append(line)
            else:
                mkSubFileByString(buf, filename, insertStr)
                insertStr = re.findall(r'INSERT INTO "ZLHIS"."(.+?)" VALUES', line)[0]
                buf = []
                buf.append(line)
    finally:
        mkSubFileByString(buf, filename, insertStr)
        fin.close()


if __name__ == '__main__':
    begin = time.time()
    splitByLineString(r'F:\chen\pythonworkspace\PythonGithub\pythonToolMethod\test.sql', "AAA")
    end = time.time()
    print('time is %d seconds ' % (end - begin))
